package com.program.facesigninsystem.controller;

import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.entity.SignInActivity;
import com.program.facesigninsystem.service.ISignInActivityService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@CrossOrigin
@RestController
@RequestMapping(value = "/act")
public class SignInActivityController {
    @Autowired
    ISignInActivityService iSignInActivityService;
    @RequestMapping(value = "/pubNewAct",method = RequestMethod.POST)
    private ResponseModel addNewActivity(@RequestBody JSONObject act){
        try{
            System.out.println(act.toString());
            String actId = "";
            Random random = new Random();
            for (int i = 0; i < 20; i++) {
                actId += String.valueOf(random.nextInt(10));
            }
            iSignInActivityService.addNewActivity(
                    actId,
                    act.getString("title"),
                    act.getString("actType"),
                    act.getString("caption"),
                    act.getString("cid"),
                    act.getString("tid")
            );
            Object[] listSession = act.getJSONArray("listSession").toArray();
            for(int i = 0; i < listSession.length; i++) {
                String lid = "";
                for (int j = 0; j < 20; j++) {
                    lid += String.valueOf(random.nextInt(10));
                }
                JSONObject temp = JSONObject.fromObject(listSession[i]);
                iSignInActivityService.addNewLink(
                        lid,
                        temp.getInt("linkType"),
                        temp.getString("duration"),
                        temp.getString("startTime"),
                        temp.getString("endTime"),
                        temp.getInt("status"),
                        actId
                );
            }
            return new ResponseModel(200,"发起成功");
        }catch (Exception e){
            System.out.println(e);
            return new ResponseModel(168,"发起失败");
        }
    }

    @RequestMapping(value = "/findActByTid",method = RequestMethod.GET)
    private  ResponseModel findActByTid(@RequestParam("tid")String tid){
        Object[] activityList = iSignInActivityService.selectActivityByTid(tid);
        ArrayList data = new ArrayList();
        try{
            for(int i = 0;i<activityList.length;i++) {
                JSONObject act = JSONObject.fromObject(activityList[i]);
                act.accumulate("className",act.getString("cid"));
                act.remove("cid");
                act.remove("tid");
                Object[] linkList = iSignInActivityService.selectLinkByActId(act.getString("actId"));
                ArrayList temp = new ArrayList();
                for(int j = 0;j<linkList.length;j++) {
                    JSONObject link = JSONObject.fromObject(linkList[j]);
                    link.remove("actId");
                    temp.add(link);
                }
                act.accumulate("links",temp);
                data.add(act);
            }
            return new ResponseModel(200,"获取成功",data);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseModel(145,"获取失败");
        }
    }
}
